package com.buba.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import entity.maintenance.MaintenanceLevel;
import com.buba.mapper.MaintenanceLevelMapper;
import com.buba.service.MaintenanceLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import entity.maintenance.MaintenanceProgram;
import entity.maintenance.dto.MaintenanceLevelDto;
import entity.maintenance.dto.MaintenanceLevelQueryWrapper;
import entity.system.CodeRules;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import utils.pageutil.MyPageHelper;
import utils.pageutil.PageRequest;
import utils.pageutil.PageResult;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 保养级别 服务实现类
 * </p>
 *
 * @author beisheng
 * @since 2023-05-18
 */
@Service
public class MaintenanceLevelServiceImpl extends ServiceImpl<MaintenanceLevelMapper, MaintenanceLevel> implements MaintenanceLevelService {

    @Autowired
    private MaintenanceLevelMapper maintenanceLevelMapper;
    @Autowired
    private feign.systemFeign systemFeign;

    @Override
    public List<MaintenanceLevel> queryAll() {
        QueryWrapper<MaintenanceLevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_flag", 0);
        queryWrapper.eq("level_state", 1);
        List<MaintenanceLevel> list = maintenanceLevelMapper.selectList(queryWrapper);
        return list;
    }


    /**
     * @param maintenanceLevelDto
     * @return
     */
    @Override
    public PageResult selMaintenanceLevel(MaintenanceLevelDto maintenanceLevelDto) {
        MaintenanceLevelQueryWrapper maintenanceLevelQueryWrapper = new MaintenanceLevelQueryWrapper();
        QueryWrapper<MaintenanceLevel> conditional = maintenanceLevelQueryWrapper.conditional(maintenanceLevelDto);
        PageRequest pageRequest = new PageRequest(maintenanceLevelDto.getPage(), maintenanceLevelDto.getPageSize());
        MyPageHelper.startPage(pageRequest);
        List<MaintenanceLevel> list = maintenanceLevelMapper.selectList(conditional);
        PageResult pageResult = MyPageHelper.getPageResult(list);
        return pageResult;
    }

    /**
     * 添加保养级别信息
     *
     * @param maintenanceLevel
     * @return
     */
    @Override
    public boolean insMaintenanceLevel(MaintenanceLevel maintenanceLevel) {
        //根据规则生成编号
        CodeRules byxm = systemFeign.selAllRules("BYJB");
        //查询时间格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(byxm.getDateTime());
        //拼接所需时间字符串
        String format = simpleDateFormat.format(new Date());
        //当前表长度
        int size = maintenanceLevelMapper.selectList(new QueryWrapper<MaintenanceLevel>().eq("delete_flag", 0)).size();
        //生成编码
        String code = byxm.getPrefix() + format + "0" + (size + 1);
        maintenanceLevel.setLevelCoding(code);
        maintenanceLevel.setCreateTime(new Date());
        maintenanceLevel.setUpdateTime(new Date());

        maintenanceLevel.setDeleteFlag(0);
        int insert = maintenanceLevelMapper.insert(maintenanceLevel);
        return insert > 0;
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public boolean delMaintenanceLevel(List<Integer> ids) {
        int i = maintenanceLevelMapper.deleteBatchIds(ids);
        return i>0;
    }

    /**、
     * 编辑保养等级
     * @param maintenanceLevel
     * @return
     */
    @Override
    public boolean updateMaintenanceLevel(MaintenanceLevel maintenanceLevel) {
        maintenanceLevel.setUpdateTime(new Date());
        UpdateWrapper<MaintenanceLevel> UpdateWrapper = new UpdateWrapper<>();
        UpdateWrapper.eq("Level_id", maintenanceLevel.getLevelId());
        UpdateWrapper.eq("delete_flag",0);
        int update = maintenanceLevelMapper.update(maintenanceLevel, UpdateWrapper);
        return update>0;
    }
}
